$(function(){
	getAllUsers(1);
	
})

function getAllUsers(pageNum){

	$.post("getAllUsers.do", {pageNum:pageNum}, function(data) {
		var userList = JSON.parse(data);
		var page = userList[0];
		var baseNum = (page.currentPage - 1) * page.eachPageCount;
		console.log(userList);
		var tbody = $("#tbody-allUsers");
		tbody.empty();
		for (var i = 1; i < userList.length; i++) {
			var user = 
			"<tr>"+
			"<td id='td_orderNum'>"+(i+page.eachPageCount*(page.currentPage-1))+"</td>"+
			"<td id='td_username'>"+userList[i].username+"</td>"+
			"<td id='td_name'>"+userList[i].name+"</td>"+
			"<td id='td_employeeNum'>"+userList[i].employeeNum+"</td>"+
			"<td id='td_department'>"+userList[i].department+"</td>"+
			"<td id='td_sex'>"+userList[i].sex+"</td>"+
			"<td id='td_birthday'>"+userList[i].birthday+"</td>"+
			"<td id='td_phoneNum'>"+userList[i].phoneNum+"</td>"+
			"<td id='td_email'>"+userList[i].email+"</td>"+
			"<td id='td_registerTime'>"+userList[i].registerTime+"</td>"+
			"<td id='td_operation'>删除</td>"+
			"</tr>";

			tbody.append(user);
		}

		// 为上面添加的gauge添加click事件
		$(".users").each(function() {
			$(this).click(function() {
				console.log("12")
				if ($(this).hasClass("user-selected")) {
					$(this).removeClass("user-selected");

				} else {
					$(this).addClass("user-selected");

				}
			})
		})

		var pagination = $("#pagination");
		pagination.empty();

		var first = "<li id='first_page' page_num='1'><a class='btn btn-default' href='#' role='button'>首页</a></li>"
		var previous = "<li id='previous_page'><a href='#' aria-label='Previous'> <span aria-hidden='true'>&laquo;</span></a></li>"
		pagination.append(first);
		pagination.append(previous);
		
		for (var i = page.currentPage - 2,j=i; i <= page.totalPage; i++) {
			
			if (i < 1){
				i = 1;
				j=i;
			}else if(page.totalPage-j<=4 && page.totalPage>5){
				i=page.totalPage-4;
				j=0;
			}
			var li = "<li class='page_number' page_num=" + i + "><a href='javascript:void(0)'>" + i + "</a></li>"
			pagination.append(li);
			if (page.currentPage >= 3) {
				if (i == page.currentPage + 2)
					break;
			} else {
				if (i == 5)
					break;
			}
		}
		
		
		
		
		pagination.find(".page_number").removeClass("active");
		pagination.find(".page_number[page_num='" + page.currentPage + "']").addClass("active");
		var next = "<li id='next_page'><a href='#' aria-label='Next'> <span aria-hidden='true'>&raquo;</span></a></li>";
		var last = "<li id='last_page' page_num=" + page.totalPage + "><a class='btn btn-default' href='#' role='button'>尾页</a></li>"
		pagination.append(next);
		pagination.append(last);

		if (page.currentPage == 1) {
			$("#first_page").addClass("disabled")
			$("#previous_page").addClass("disabled")
		} else {
			$("#first_page").removeClass("disabled")
			$("#previous_page").removeClass("disabled")
		}
		if (page.totalPage == page.currentPage) {

			$("#next_page").addClass("disabled")
			$("#last_page").addClass("disabled")
		} else {
			$("#next_page").removeClass("disabled")
			$("#last_page").removeClass("disabled")
		}
		pagination.find("li").each(function() {
			$(this).click(function() {
				var currentPage;
				if ($(this).attr("id") == "previous_page") {
					currentPage = (page.currentPage - 1) < 1 ? 1 : page.currentPage - 1;
				} else if ($(this).attr("id") == "next_page") {
					currentPage = (page.currentPage + 1) > page.totalPage ? page.totalPage : page.currentPage + 1;

				} else {
					currentPage = $(this).attr("page_num");
				}
				getAllUsers(currentPage);
			})
		})

	})

}